package com.alibaba.otter.manager.web.dto;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.slf4j.MDC;

@Getter
@Setter
@ToString(callSuper = true)
public class RestResponse<T> {

    private final static String SUCCESS = "success";

    private Integer code;

    private String msg;

    private T data;

    private String cause;

    public String getTraceId() {
        return MDC.get("traceId");
    }

    public RestResponse() {}

    public RestResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RestResponse(Integer code, String msg, T data, String causeBy) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.cause = causeBy;
    }

    /**
     * 成功返回静态方法.
     *
     * @param msg  提示消息
     * @param data body数据
     * @param <T>  泛型
     */
    public static <T> RestResponse<T> ok(String msg, T data) {
        return new RestResponse<>(200, msg, data);
    }

    /**
     * 成功返回静态方法.
     *
     */
    public static <T> RestResponse<T> ok() {
        return new RestResponse<>(200, SUCCESS, null);
    }
    /**
     * 调用失败, 返回500.
     *
     */
    public static <T> RestResponse<T> fail(int code,String msg) {
        return new RestResponse<>(code, msg, null);
    }

    /**
     * 成功返回静态方法.
     *
     * @param data body数据
     * @param <T>  泛型
     */
    public static <T> RestResponse<T> ok(T data) {
        return new RestResponse<>(200, SUCCESS, data);
    }

    /**
     * 调用失败, 返回500.
     *
     * @param msg  提示下戏
     * @param data body数据
     * @param <T>  泛型
     */
    public static <T> RestResponse<T> fail(String msg, T data) {
        return new RestResponse<>(500, msg, data);
    }


    public boolean isSuccess(){
        return code == 200;
    }


}
